package com.zj.osm.utils;

/**
 * <pre>
 *     author : luhenchang
 *     e-mail : 1276998208@qq.com
 *     time   : 2020/01/10
 *     desc   : 版权所有
 *     version: 1.0
 * </pre>
 */
public  class CoordinateTool
{
    static double a = 6378137;
    static double ee = 0.00669437999013;
    private static double X_PI = Math.PI * 3000.0 / 180.0;

    /// <summary>
    /// BD09=>GCJ-02 百度坐标系=>火星坐标系
    /// </summary>
    /// <param name="glat"></param>
    /// <param name="glon"></param>
    /// <returns></returns>
    public static Location bd092GCJ(double glat, double glon)
    {
        double x = glon - 0.0065;
        double y = glat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
        return new Location(z * Math.sin(theta), z * Math.cos(theta));
    }

    /// <summary>
    /// 百度===>84
    /// </summary>
    /// <param name="glat"></param>
    /// <param name="glon"></param>
    /// <returns></returns>
    public static Location dbToWGS84(double glat, double glon)
    {
        Location location1 = bd092GCJ(glat, glon);
        return TransformToGoogle(location1.lng, location1.lat);
    }

    /// <summary>
    /// 火星===>WGS-84
    /// </summary>
    /// <param name="Lng"></param>
    /// <param name="Lat"></param>
    /// <returns></returns>
    public static Location TransformToGoogle(double Lng, double Lat)
    {
        double tlng = (double)Lng;
        double tlat = (double)Lat;

        if (outOfChina(tlat, tlng))
        {
            return new Location(tlat, tlng);
        }
        double dLat = transformLat(tlng - 105.0, tlat - 35.0);
        double dLon = transformLon(tlng - 105.0, tlat - 35.0);
        double radLat = tlat / 180.0 * Math.PI;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * Math.PI);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * Math.PI);
        return new Location(tlat + dLat, tlng + dLon);
    }

    private static double transformLat(double x, double y)
    {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0 * Math.PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y * Math.PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    private static double transformLon(double x, double y)
    {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0 * Math.PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x / 30.0 * Math.PI)) * 2.0 / 3.0;
        return ret;
    }

    private static boolean outOfChina(double lat, double lon)
    {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }

    public static class Location{
        private double lat;
        private double lng;

        public Location(double lat, double lng) {
            this.lat = lat;
            this.lng = lng;
        }

        public double getLat() {
            return lat;
        }

        public void setLat(double lat) {
            this.lat = lat;
        }

        public double getLng() {
            return lng;
        }

        public void setLng(double lng) {
            this.lng = lng;
        }

        @Override
        public String toString() {
            return "Location{" +
                    "lat=" + lat +
                    ", lng=" + lng +
                    '}';
        }
    }

}
